---@class WenheYanbing.Player : Base.Player
---@field public kingdom string
---@field public tavernTier integer
---@field public gold integer
---@field public health integer
---@field public board WenheYanbing.Minion[]
---@field public hand WenheYanbing.Minion[]
---@field public tavernMinions WenheYanbing.Minion[]
---@field public freezeTavern boolean
---@field public room WenheYanbing.RoomBase
local Player = Fk.Base.Player:subclass("WenheYanbing.Player")

function Player:initialize()
    Fk.Base.Player.initialize(self)
    
    self.kingdom = "qun"
    self.tavernTier = 1
    self.gold = 5
    self.health = 30
    self.board = {}
    self.hand = {}
    self.tavernMinions = {}
    self.freezeTavern = false
    self.room = nil
end

function Player:serialize()
    local o = Fk.Base.Player.serialize(self)
    o.kingdom = self.kingdom
    o.tavernTier = self.tavernTier
    o.gold = self.gold
    o.health = self.health
    o.board = self:serializeMinions(self.board)
    o.hand = self:serializeMinions(self.hand)
    o.tavernMinions = self:serializeMinions(self.tavernMinions)
    o.freezeTavern = self.freezeTavern
    return o
end

function Player:deserialize(o)
    Fk.Base.Player.deserialize(self, o)
    self.kingdom = o.kingdom
    self.tavernTier = o.tavernTier
    self.gold = o.gold
    self.health = o.health
    self.board = o.board or {}
    self.hand = o.hand or {}
    self.tavernMinions = o.tavernMinions or {}
    self.freezeTavern = o.freezeTavern or false
end

-- 序列化随从数据
function Player:serializeMinions(minions)
    local result = {}
    for _, minion in ipairs(minions) do
        if type(minion) == "table" then
            table.insert(result, {
                id = minion.id,
                name = minion.name,
                tier = minion.tier,
                attack = minion.attack,
                health = minion.health,
                kingdom = minion.kingdom,
                skills = minion.skills,
                cost = minion.cost,
                isGolden = minion.isGolden or false,
                hasTaunt = minion.hasTaunt or false
            })
        end
    end
    return result
end

-- 设置房间引用
function Player:setRoom(room)
    self.room = room
end

-- 获取升级费用
function Player:getUpgradeCost()
    if not self.room or not self.room.engine then
        return 0
    end
    return self.room.engine:getUpgradeCost(self.tavernTier)
end

-- 升级酒馆
function Player:upgradeTavern()
    local cost = self:getUpgradeCost()
    if self.gold >= cost and self.tavernTier < 6 then
        self.gold = self.gold - cost
        self.tavernTier = self.tavernTier + 1
        return true
    end
    return false
end

-- 刷新酒馆
function Player:refreshTavern()
    if self.gold < 1 then
        return false
    end
    
    self.gold = self.gold - 1
    
    if not self.freezeTavern then
        self.tavernMinions = {}
        
        -- 安全地获取可用随从
        local availableMinions = {}
        if self.room and self.room.engine then
            availableMinions = self.room.engine:getMinionsByTier(self.tavernTier) or {}
        else
            -- 如果没有引擎，使用默认随从
            availableMinions = self:getDefaultMinions()
        end
        
        -- 随机抽取5个随从
        for i = 1, 5 do
            if #availableMinions > 0 then
                local randomIndex = math.random(1, #availableMinions)
                local minion = availableMinions[randomIndex]
                table.insert(self.tavernMinions, minion)
            end
        end
    end
    
    self.freezeTavern = false
    return true
end

-- 获取默认随从（当引擎不可用时）
function Player:getDefaultMinions()
    return {
        { id = 1, name = "默认随从1", tier = 1, attack = 1, health = 1, kingdom = "qun", cost = 3 },
        { id = 2, name = "默认随从2", tier = 1, attack = 1, health = 2, kingdom = "qun", cost = 3 },
        { id = 3, name = "默认随从3", tier = 1, attack = 2, health = 1, kingdom = "qun", cost = 3 },
    }
end

-- 冻结酒馆
function Player:freezeTavern()
    self.freezeTavern = not self.freezeTavern
    return true
end

-- 购买随从
function Player:buyMinion(tavernIndex)
    if tavernIndex < 1 or tavernIndex > #self.tavernMinions then
        return false
    end
    
    local minion = self.tavernMinions[tavernIndex]
    if self.gold >= minion.cost then
        self.gold = self.gold - minion.cost
        table.insert(self.hand, minion)
        table.remove(self.tavernMinions, tavernIndex)
        return true
    end
    return false
end

-- 出售随从
function Player:sellMinion(boardPosition)
    if boardPosition < 1 or boardPosition > #self.board then
        return false
    end
    
    local minion = table.remove(self.board, boardPosition)
    self.gold = self.gold + 1
    return true
end

-- 放置随从到战场
function Player:placeMinion(handIndex, boardPosition)
    if handIndex < 1 or handIndex > #self.hand or #self.board >= 7 then
        return false
    end
    
    local minion = table.remove(self.hand, handIndex)
    if boardPosition > #self.board then
        table.insert(self.board, minion)
    else
        table.insert(self.board, boardPosition, minion)
    end
    return true
end

return Player